Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
@esbuild-kit/cjs-loader
Advanced tools
Node.js loader for compiling ESM & TypeScript modules to CommonJS
@esbuild-kit/cjs-loader is an npm package that allows you to use ES modules (ESM) in CommonJS (CJS) environments. It leverages esbuild to transpile ESM to CJS on the fly, enabling seamless integration of modern JavaScript modules in older Node.js projects.
Transpile ESM to CJS
This feature allows you to transpile ES modules to CommonJS on the fly. By registering the loader, you can require ESM files in a CJS environment.
const { register } = require('@esbuild-kit/cjs-loader');
register();
const esmModule = require('./esmModule.mjs');
console.log(esmModule);
Custom Loader Options
You can customize the loader options such as the target ECMAScript version and the output format. This provides flexibility in how the ESM is transpiled to CJS.
const { register } = require('@esbuild-kit/cjs-loader');
register({ target: 'es2017', format: 'cjs' });
const esmModule = require('./esmModule.mjs');
console.log(esmModule);
The 'esm' package allows you to use ES modules in Node.js without the need for a build step. It provides a seamless way to load ESM in a CJS environment, similar to @esbuild-kit/cjs-loader, but without leveraging esbuild for transpilation.
The 'babel-register' package hooks into Node.js require to transpile files on the fly using Babel. It supports a wide range of JavaScript syntax transformations, including ESM to CJS, but may have a larger performance overhead compared to @esbuild-kit/cjs-loader.
The 'ts-node' package allows you to run TypeScript files directly in Node.js. It can also handle ESM to CJS transpilation when used with appropriate TypeScript configurations. It is more focused on TypeScript but can be used for similar purposes.
Node.js require()
hook to instantaneously transform ESM & TypeScript to CommonJS on demand using esbuild.
.cjs
& .mjs
(.cts
& .mts
)node:
import prefixesTip:
cjs-loader doesn't hook into dynamic
import()
calls.Use this with esm-loader for
import()
support. Alternatively, use tsx to handle them both automatically.
npm install --save-dev @esbuild-kit/cjs-loader
Pass @esbuild/cjs-loader
into the --require
flag
node -r @esbuild/cjs-loader ./file.js
The following properties are used from tsconfig.json
in the working directory:
jsxFactory
jsxFragmentFactory
Modules transformations are cached in the system cache directory (TMPDIR
). Transforms are cached by content hash so duplicate dependencies are not re-transformed.
Set environment variable ESBK_DISABLE_CACHE
to a truthy value to disable the cache:
ESBK_DISABLE_CACHE=1 node -r @esbuild/cjs-loader ./file.js
tsx - Node.js runtime powered by esbuild using @esbuild-kit/cjs-loader
and @esbuild-kit/esm-loader
.
@esbuild-kit/esm-loader - TypeScript to ESM transpiler using the Node.js loader API.
FAQs
Node.js loader for compiling ESM & TypeScript modules to CommonJS
We found that @esbuild-kit/cjs-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.